package com.woniuxy02;

import java.util.Scanner;
import java.util.TreeSet;

/**
 * @author 作者
 * @create 2025-10-28 10:53
 */
public class MyUtil {
    //访问修饰符 返回值类型 方法名(方法参数)
    //定义方法：无参方法(没有参数)
    //返回值类型:void没有返回值

    public void printStart00() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public void printStart01() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入你要打印的符号");
        String str = scanner.next();
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(str);
            }
            System.out.println();
        }
    }

    //带参方法：参数就是一个变量
    //这个方法的功能就是打印这个参数字符:具体的字符串的值是什么
    //定义方法时（）中的参数:形式参数
    public void printStart02(String str) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(str);
            }
            System.out.println();
        }
    }

    public void printStart03(int row, String str) {
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(str);
            }
            System.out.println();
        }
    }

    //方法就是功能，调用方法就是执行功能
    public void wash01() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("放入你要洗的衣物");
        String str = scanner.next();
        System.out.println("wash--" + str);
    }

    //方法就是一个功能
    //参数就是参与操作的数据
    public void wash02(String str) {
        System.out.println("wash--" + str);
    }

    public void testInt(int x) {
        System.out.println("进入testInt方法x=" + x);
        x = x + 10;
        System.out.println("进入testInt方法，+10,x=" + x);
    }

    public void testArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = a[i] + 10;
        }
    }

    //没有返回值的方法，使用void,可以有return,return后面不能有值
    public void testArray02(int[] a) {
        a = new int[3];
        for (int i = 0; i < a.length; i++) {
            a[i] = a[i] + 10;
        }
        return;
    }

    //定义方法需要指定返回值类型，这个类型要和return后面值的类型一致
    //一个方法的返回值只能有一个
    public int add(int a, int b) {
        int r = a + b;
        //return:反调用结束以后，给用户一个返回值
        return r;
        //System.out.println("加法计算结果："+a+"+"+b+"="+r);
    }


    public int febonacci(int n) {
        //使用数组，不用数组
        if (n == 1 || n == 2) {
            return 1;
        } else {
            int f1 = 1, f2 = 1, r = 0;
            for (int i = 3; i <= n; i++) {
                r = f1 + f2;
                f1 = f2;
                f2 = r;
            }
            return r;
        }
    }

    public int febonacci2(int n) {
        //使用数组，不用数组
        int r = 0;
        if (n == 1 || n == 2) {
            r = 1;
        } else {
            int f1 = 1, f2 = 1;
            for (int i = 3; i <= n; i++) {
                r = f1 + f2;
                f1 = f2;
                f2 = r;
            }
        }
        return r;
    }


    public boolean isZhiShu(int num) {
        boolean flag = true;
        for (int i = 2; i <= num - 1; i++) {
            if (num % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }


    public int findIndex(int[] a, int num) {
        int index = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] == num) {
                index = i;
                break;
            }
        }
        return index;
    }


    public int[] sort1(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
        return a;
    }

    public void sort2(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    int t = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = t;
                }
            }
        }
    }

    //定义一个方法，数据按照格式显示
    public String arrayToString(int[] a) {
        String str = "[";
        for (int i = 0; i < a.length; i++) {
            str = str + a[i];
            if (i != a.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        return str;
    }

    public void arrayToString2(int[] a) {
        String str = "[";
        for (int i = 0; i < a.length; i++) {
            str = str + a[i];
            if (i != a.length - 1) {
                str += ", ";
            }
        }
        str += "]";
        System.out.println(str);
    }

    public int[] arrayLarge(int[] a) {
        int[] b = new int[a.length + 1];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        a = b;
        return a;
    }

    public int[] arrayInsert(int[] a, int num, int insertIndex) {
        int zeroIndex = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] == 0) {
                zeroIndex = i;
                break;
            }
        }
        //没有0，要扩容
        if (zeroIndex == -1) {
            int[] b = new int[a.length + 1];
            for (int i = 0; i < a.length; i++) {
                b[i] = a[i];
            }
            a = b;
        }
        //移动元素,从倒数第二位开始，到insertIndex，移动元素
        for (int i = a.length - 2; i >= insertIndex; i--) {
            a[i + 1] = a[i];
        }
        //插入元素
        a[insertIndex] = num;
        return a;
    }

    public void reverse(int[] a) {
        int t;
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }

}
